/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

export enum MetricEventType {
  DURATION = 'duration',
  INSTANT = 'instant',
  COUNTER = 'counter',
  GAUGE = 'gauge',
  OBJECT = 'object',
  METADATA = 'metadata',
  MARK = 'mark',
  LOG = 'log',
  CONTINUAL = 'continual',
}

export class EventHead {
  id: string;
  name: string;
  description: string;
  type: MetricEventType;

  constructor(id: string, name: string, description: string, type: MetricEventType) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.type = type;
  }
}

export class EventBody {
  pid: number;
  tid: string;
  startTime: number;
  endTime?: number;
  totalTime?: number;

  constructor(pid: number, tid: string) {
    this.pid = pid;
    this.tid = tid;
    this.startTime = Number(process.hrtime.bigint());
  }
}

export type EventAdditional = object;

export class BaseEvent {
  protected head: EventHead;
  protected body: EventBody;
  protected additional: EventAdditional;

  constructor(head: EventHead, body: EventBody) {
    this.head = head;
    this.body = body;
    this.additional = {};
  }

  setStartTime(time?: number) {
    this.body.startTime = time ?? Number(process.hrtime.bigint());
  }

  setEndTime(time?: number) {
    this.body.endTime = time ?? Number(process.hrtime.bigint());
  }

  setTotalTime(time: number) {
    this.body.totalTime = time;
  }

  getId() {
    return this.head.id;
  }

  getName() {
    return this.head.name;
  }

  getDescription() {
    return this.head.description;
  }

  setName(name: string) {
    this.head.name = name;
  }

  getType() {
    return this.head.type;
  }

  setType(type: MetricEventType) {
    this.head.type = type;
  }

  getTid() {
    return this.body.tid;
  }

  setTid(tid: string) {
    this.body.tid = tid;
    return this;
  }
}
